package cn.com.huzo.webapp.controller.master;

import cn.com.huzo.model.entities.master.DepartmentEntity;
import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.EmployeeEntity;
import cn.com.huzo.service.master.DepartmentManager;
import cn.com.huzo.service.master.EmployeeManager;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 人员信息控制器
 *
 * @author 吴旭
 */
@Controller
public class EmployeeController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "EmployeeManager")
    private EmployeeManager _manager;

    @Resource(name = "DepartmentManager")
    private DepartmentManager _manager_dept;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到人员一览画面
     * <p>
     * @return
     */
    @RequestMapping("master/employee/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.employee.list.def");
        return view;
    }

    /**
     * 员工编辑画面
     * <p>
     * @param action 操作类别 1：增加 2：编辑
     * @param employeeId 员工标识
     * @param nodeId 所属目录标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("master/employee/{action}/{employeeId}/{nodeId}/edit.html")
    public ModelAndView redirectEditWnd(@PathVariable(value = "action") int action,
            @PathVariable(value = "employeeId") Long employeeId,
            @PathVariable(value = "nodeId") Long nodeId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("employeeId", employeeId);

        if (nodeId != 0L) {
            DepartmentEntity _item = _manager_dept.fetchByKey(nodeId);
            view.addObject("nodeId", nodeId);
            view.addObject("nodeText", _item.getParentName() + " → " + _item.getDepartmentName());
        }

        view.setViewName("master.employee.edit.def");
        return view;
    }

    /**
     * 弹出员工选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/employee/choice.html")
    public ModelAndView redirectEmployeeChoiceWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.employee.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 根据标识取得人员数据。
     *
     * @param criterion 人员标识、当前用户、流程标识
     * @return 人员数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/employee/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    EmployeeEntity fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByKey(MapUtils.getLong(criterion, "HeaderId"));
    }

    /**
     * 根据条件取得人员信息分页数据。
     * <p>
     * @param filters 检索条件
     * @return 人员信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/employee/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> filters) throws Exception {
        return _manager.fetchForPage(filters);
    }

    /**
     * 保存人员信息数据。
     * <p>
     * @param instance 要保存的人员信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "master/employee/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody EmployeeEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getEmployeeId() > 0) { //编辑
            rowsAffected = (long) _manager.update(instance);
        } else {//新增
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/employee/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }

    /**
     * 批量更新人员所属部门。
     *
     * @param itemList 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/employee/changeCatalogue.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer changeCatalogue(@RequestBody final Map<String, ? extends List<EmployeeEntity>> itemList) throws Exception {
        int rowsAffected = 0;
        if (itemList.get("updateList").size() > 0) {
            rowsAffected = _manager.changeCatalogue(itemList.get("updateList"));
        }

        return rowsAffected;
    }
    // </editor-fold>
}
